import router from '~/router'
import store from '~/store'
import { createApp } from 'vue'
import App from './App.vue'
// import 'require/bin/require-command'
const app = createApp(App)
import { getUSerInfo } from "~/api/xm.js"
import { getToken } from '~/composables/auth'
import adminLayout from "~/layouts/admin.vue"
import yonghu from '~/pages/bbx/yonghu.vue'
import fenlei from '~/pages/bbx/fenlei.vue'
import video from '~/pages/bbx/video.vue'
import timu  from '~/pages/bbx/timu.vue'
// 是否已经加载了管理员信息
// 全局路由守卫
router.beforeEach((to, from, next) => {
        const token = getToken()
        console.log(token);
        if (token) {
            if (to.path === '/login') {

                console.log(store.state.user);
                if (store.state.user.type == 2) {
                    next('/chongwujiankang');
                } else {
                    next('/chongwu');
                }
            } else {
                let menus = store.state.menus;
                let user = store.state.user;
                console.log('menus', menus);
                if (menus.length && user.id) {
                    console.log("存在路由");
                    next();
                } else {
                    console.log("动态加载路由");
                    store.dispatch('getInfo', token).then(res => {
                        console.log("------------", res)
                        let routeList = []
                      if (res.type == '3') {
                            console.log("3");
                            routeList = [{
                                    path: '/yonghu',
                                    name: '用户信息管理',
                                    component: yonghu,
                                    meta: {
                                        title: '报销管理'
                                    }
                                },
                          {
                                    path: '/fenlei',
                                    name: '分类管理',
                                    component: fenlei,
                                    meta: {
                                        title: '报销管理'
                                    }
                                },
                                {
                                    path: '/timu',
                                    name: '题目管理',
                                    component: timu,
                                    meta: {
                                        title: '报销管理'
                                    }
                                },
                          {
                                    path: '/video',
                                    name: '视频管理',
                                    component: video,
                                    meta: {
                                        title: '报销管理'
                                    }
                                },

                            ]
                        }
                        console.log("routeList", routeList);
                        store.commit('SET_MENUS', routeList)
                        router.addRoute({
                            path: "/",
                            component: adminLayout,
                            children: routeList
                        })
                        router.push({...to, replace: true })
                    });
                }
            }
        } else {
            console.log(to.path);
            if (to.path === '/login') {
                console.log('登录页面');
                next();
            } else {
                next('/login');
            }
        }

    })
    // 设置页面标题
    // let title = (to.meta.title ? to.meta.title : "")
    // document.title = title
    // router.afterEach(() => hideFullLoading())
    // let routeList = []
    // if (res == 0) {
    //     console.log("学生");
    //     routeList = [{
    //             path: '/chakanZuoye',
    //             name: '查看作业',
    //             component: chakanZuoye,
    //             meta: {
    //                 title: '查看作业'
    //             }
    //         },
    //         {
    //             path: '/chakanChengji',
    //             name: '查看成绩',
    //             component: chakanChengji,
    //             meta: {
    //                 title: '查看成绩'
    //             }
    //         }
    //     ]
    // } else {
    //     console.log("老师");
    //     routeList = [{
    //             path: '/chakanBanji',
    //             name: '查看班级',
    //             component: chakanBanji,
    //             meta: {
    //                 title: '查看班级'
    //             }
    //         },
    //         {
    //             path: '/pigaiZuoye',
    //             name: '批改作业',
    //             component: pigaiZuoye,
    //             meta: {
    //                 title: '批改作业'
    //             }
    //         },
    //         {
    //             path: '/xueshengg',
    //             name: '学生信息',
    //             component: xueshengg,
    //             meta: {
    //                 title: '学生信息'
    //             }
    //         }
    //     ]
    // }
    // console.log("routeList", routeList);
    // store.commit('SET_MENUS', routeList)
    // router.addRoute({
    //         path: "/",
    //         component: adminLayout,
    //         children: routeList
    //     })
    // router.push({...to, replace: true })